<template>
  <div class="main">
    <div class="top_content">
      <rt-alert>11</rt-alert>
      <!-- <img :src="imgUrl + img" /> -->
      <div class="down_time" @click="skipMe">跳过 {{ time }}</div>
      <div class="to_detail" @click="toDetail">
        <div style="margin-right: 10px">
          点击查看
          <img src="../../assets/imgs/right.png" />
        </div>
      </div>
    </div>
    <div class="logo_title">
      <img src="../../assets/imgs/logo.png" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'Startpage',
  data() {
    return {
      goFormStart: true,
      time: 5,
      img: '',
      id: '',
      isPause: false,
      dataList: []
    };
  },
  created() {
    this.timer();
    this.fetchData();
  },
  methods: {
    timer() {
      let t = 5;
      let timecount = setInterval(() => {
        t--;
        this.time = t;
        if (t <= 0) {
          clearInterval(timecount);
          if (!this.isPause) {
            this.$router.push({
              name: 'article-page'
            });
          }
        }
      }, 1000);
    },
    fetchData() {
      return this.getDataList().then(resp => {
        this.dataList = resp;
        if (this.dataList) {
          var index = Math.floor(Math.random() * this.dataList.length);
          const data = this.dataList[index];
          this.img = data.avatar;
          this.id = data.bbs_campaignid;
        }
        return resp;
      });
    },
    getDataList() {
      return new Promise(resolve => {
        rt.get('/api/bbs/campaign/page/start').then(res => {
          resolve(res);
        });
      });
    },
    skipMe() {
      this.isPause = true;
      this.$router.push({
        name: 'article-page'
      });
    },
    toDetail() {
      this.isPause = true;
      this.$router.replace({
        name: 'campaign-detail',
        query: { id: this.id, goFormStart: this.goFormStart }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.main {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  .top_content {
    display: block;
    flex: 1;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    .down_time {
      opacity: 0.8;
      position: absolute;
      top: 50px;
      right: 30px;
      width: 90px;
      height: 40px;
      line-height: 40px;
      font-size: 18px;
      color: #ffffff;
      text-align: center;
      border-radius: 20px;
      background: #606266;
    }
    .to_detail {
      opacity: 0.8;
      position: relative;
      bottom: 15%;
      width: 150px;
      height: 50px;
      margin: 0 auto;
      line-height: 50px;
      font-size: 18px;
      color: #ffffff;
      text-align: center;
      border-radius: 20px;
      background: #606266;
      img {
        width: 20px;
        height: 20px;
        top: 30%;
        color: #fff;
        position: absolute;
        vertical-align: middle;
      }
    }
  }
  .logo_title {
    padding: 20px;
    text-align: center;
    margin: 0 auto;
    width: 50%;
  }
}
</style>
